Liberty Basic Beginner Series - Part 6

Introduction, Bugs & Keeping Score

© 2003, Brad Moore

author website:

LB Connection

Beginning Programming Series - Part 6

Introduction -

I have had some really great comments recently regarding this series. I really appreciate the kind words. Of course this series is for people who are interested in learning to program in Liberty Basic and are basically starting at go with little or no programming knowledge. That is not to say that experienced programmers can't learn from the series, but it may not offer a lot of challenge - it may, however, refresh ones memory on different aspects of the language.

As always I want to encourage the readers of the articles to follow along and try the examples and challenges. This is the sixth in a series of articles on this topic. We have been progressively building on a basic understanding of coding and the Liberty Basic language. We culminated last issue with a fairly complex game (for a beginning programmer) called Starquest. There were several opportunities for expanding the game suggested in the challenge section at the end of the last issue. I sure hope you tried some of these. I would love to see your program if you are willing to share. Drop me a line at bjaz.moore@copiasystems.com.

In this issue we will be adding a couple of the suggestions to the Starquest game. Specifically scoring, high score tracking and implementing levels. These will introduce several new concepts: File handling, state tracking and score tracking. We will also introduce the first techniques in real creating real windows program using a graphics window and displaying graphics in it. Hang on - its going to be a fun ride!

Where have we been -

One of the questions asked of me recently is a basic rundown on the commands that have been covered and where these can be found. I think this is a really great idea too, and have been thinking about it for a while. As much as I believe this does belong with the article and would love to add it, time has become a great constraint. As a result, I will not be able to add this feature right away. I will be uploading the entire series to my website in the coming months and at that time I will add the index. I will be continuing the Glossary (words in the glossary will be formatted as such: glossary term and will be hot linked to the glossary entry). As always, this installment builds on the foundations laid in earlier editions of this series. If you are just joining us and you are a beginner, I encourage you to go back and read through the previous issues. Here are links to the online archive of each:

Time to call the "Orkin Man" -

I am sure that you have heard the term "Bugs" as in "This software is full of bugs". It is a aphorism for a program that is full of errant code. The term first originated from a time when a single computer filled whole floor of huge building. It was made from vacuum tubes all forming discrete logic circuits. Moths would be drawn by the heat and land in the wiring and cause problems. The electricians would say that the problems with computer were the bugs that were in the wiring - quite literally.

Today we say programs that have unexpected problems resulting in some form of failure contain bugs. Quite often these bugs will be conditions that rarely occur and the programmer simply did not complete the code for these conditions. Sometimes bugs result from sloppy programming and poor testing. Testing is always the key though in locating and exterminating bugs in your code.

I mention this because during the course of preparing for this installment of this series I discovered a bug that I left behind in the source for Starquest. It is really easy to do. I the case of the game, it was a condition that occurred infrequently, and I did not exercise the code fully to work this bug out. My loss is you gain though.

The bug occurs when you either hit a "k" mine, or a "k" mine is placed on top of you. Everything seems to happen correctly - you ship coordinated are reset to 1,1, your energy is drained and the mine is placed, but for some reason I failed to put the spaceship symbol back to 1,1 on the map. The effect is that the ship seems to disappear from the playing area until you actually move the ship.

The error occurs in the following section:

[hitmine]
print "**********************"
print "**   B O O M ! ! !  **"
print "**********************"
print
print " ...You hit a 'K' mine!"
'subtract a life
lives = lives - 1
if lives > 0 then
   print "Your ship is returned to star position 1,1"
   print "All of your energy reserves have been drained."
   shipx = 1
   shipy = 1
   energy = 0
   print 
   input "End of move ";move;", press enter to start next move...";a$
   'increment the move counter
   move = move + 1
   'clear the screen
   cls
   goto [loop] 
end if
'Looks like the game is over!
print "All of your ships have been destroyed..."
print "You have lost this round.  Better luck next time!"
print
input "Do you want to play again ([Y] or n)?";a$
if instr(upper$(a$),"N") > 0 then
   end
else
   goto [beginAgain]
end if

To fix the problem we simply need to place the ship symbol back at 1,1 immediately after we set the ship coordinated to that location. The code looks like this:

   starmap$(1,1) = "@"

Place it in the middle of the code section show above so that it now looks like the following code when fixed:

[hitmine]
print "**********************"
print "**   B O O M ! ! !  **"
print "**********************"
print
print " ...You hit a 'K' mine!"
'subtract a life
lives = lives - 1
if lives > 0 then
   print "Your ship is returned to star position 1,1"
   print "All of your energy reserves have been drained."
   shipx = 1
   shipy = 1
   'don't forget to place the ship...
   starmap$(1,1) = "@"
   energy = 0
   print 
   input "End of move ";move;", press enter to start next move...";a$
   'increment the move counter
   move = move + 1
   'clear the screen
   cls
   goto [loop] 
end if
'Looks like the game is over!
print "All of your ships have been destroyed..."
print "You have lost this round.  Better luck next time!"
print
input "Do you want to play again ([Y] or n)?";a$
if instr(upper$(a$),"N") > 0 then
   end
else
   goto [beginAgain]
end if

Now the game should play a little bit better. So, I just have to ask: Why didn't one of you bright programmers out there email me about that little problem - huh?

What's the Score? -

Adding scoring to a game is really not that hard. You must consider the game scope, object, levels, and obstacles. The scoring mechanism needs to reflect the object of the game. Take for instance the old stand-by Missile Command. This was a great game because if involved a simple concept - shot down the incoming missiles and protect the cities. So the game designers decided to award points based on hits, number of missiles left over and cities saved. It reinforces the game concept - hit things with your missiles (and get points) conserve your missiles and make each shot count (and get more points) and save the cities (and get points).

In our game we have a couple objectives: Move through the stargate, do it in as few moves as possible and don't hit or get hit by a mine (saving lives). I will also introduce levels soon - but lets finish this first. We need to get points for using a few moves as possible, for going through the stargate and for saving our extra lives - reinforcing the theme of the game.

But how many points and for what? Well, as game designer, that is your call. What ever makes sense to you. I am thinking about 1000 points for each ship (or life) saved. 100 - points for each move less than 25, and a negative 100 points for every move more than 25. Also, as a bonus, 25 points for every gigajoule in reserve when you pass through the stargate. Levels will make higher scores possible and add more challenge to getting these scores. The other thing to consider - getting hit by a "k" mine works against you in two ways - it requires many more moves and reduces you by one ship.

So - we have a concept - now we need to consider how to write the code. Score keeping suggests tracking, so I would propose that we initialize a variable at the beginning of the program to track the score. This appears in the portion of the program where other variables which we track are initialized.

'Track some new stuff: the score 
score = 0

Next we need to calculate the results of the game play when the player passes through the stargate and wins. There is a portion in the original program in the [moveship] routine where the ship is actually moved and energy is deducted. At the end of this we check to see if we made it to the stargate. The code looks like this:

'deduct the energy first
energy = energy - drain
'now set the original ship coordinates to a period
starmap$(shipx, shipy) = "."
'now see what is in the new location - is it a mine?
if starmap$(x,y) = "k" then [hitmine]
'it must not be a mine - put the ship there
starmap$(x,y) = "@"
shipx = x
shipy = y
'tell the user about the move
print
print "You have moved your ship to star position ";shipx;",";shipy
'If this is the stargate portal - you win.
if shipx = 8 and shipy = 12 then [winner]

We are going to turn last line above (the if statement) into an IF-THEN-END IF block. To this we will add the code that calculates the players score based on the rules we outlined above. The code looks like this:

if shipx = 8 and shipy = 12 then         
   'lets calculate the score
   s = lives * 1000
   t = (25 - move)*100
   u = energy * 25
   score = score + s + t + u
   goto [winner]
end if

Finally we will enhance the winner block to display the results for us, so that we (the humans) can know what the score was, and just how well or poorly we did playing the game. We will do this in the [winner] block of code, which I am going to completely replace with the following code.

[winner]
print "     ============================   "
print "     ==                        ==   "
print "     ==      Y O U   W I N     ==   "
print "     ==                        ==   "
print "     ============================   "
print 
print "   ships remaining (";lives;") = ";s;" points"
print "      movement score (";move;" moves) = ";t;" points"
print "         remaining energy bonus (";energy;"gJ) = ";u;" points"
print 
print "   *** Your final score for the whole game is: ";score;" ***"
print
print "========================================================"
input "This game is over - do you want to play again ([Y] or n)?";a$
if instr(upper$(a$),"N") > 0 then
   end
else
   goto [beginAgain]
end if

That accomplishes one of our there goals for enhancement of the Starquest game. The entire game with all three enhancements can be found in Appendix A


Home

Tip Corner

Plot 3d

Memory Mapped File

Random File Selector

Notes for Beginners

No SoundBlaster Board

PrintInstalled Fonts

Essential Libby

API Drive Info

Begginer Series 6

Newsletter help

Index